package offer;

import java.util.ArrayList;
import java.util.List;

public class offer58one {
}
class Solution581 {
    public static String reverseWords(String s) {
        String newS = s.trim();
        String []temp = newS.split(" ");
        int j =0;
        List<String> list = new ArrayList<>();
        while (j< temp.length)
        {

            if(temp[j].length()>0)
            {
                list.add(temp[j]);
//                j++;
            }
            j++;
        }

        Object[] objects = list.toArray();
        int start = 0;
        int end = objects.length-1;
        while (start<end)
        {
           String tmp =(String) objects[start];
           objects[start] = objects[end];
           objects[end] = tmp;

            start++;
            end--;
        }
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < objects.length ; i++) {
            sb.append((String) objects[i]+" ");
        }
        String res = sb.toString().trim();
    return res;
    }

    public static void main(String[] args) {
        String s = "a good   example";
        reverseWords(s);
    }
}